import { execute } from "../../database/connector";

import { PermissionQueries } from "../queries/permission.query";
import { IPermission } from "../../models/permission.model";

/**
 * gets active permissions
 */
export const getPermissions = async () => {
  return execute<IPermission[]>(PermissionQueries.GetPermissions, []);
};

/**
 * gets active permissions based on provided id list
 */
export const getPermissionsByIds = async (ids: IPermission['id'][]) => {
    return execute<IPermission[]>(PermissionQueries.GetPermissionsByIds, [[Object.values(ids)]]);
  };

/**
 * gets a permission based on id provided
 */
export const getPermissionById = async (id: IPermission['id']) => {
  return execute<IPermission>(PermissionQueries.GetPermissionById, [id]);
};


/**
 * adds a new active permission record
 */
export const insertPermission = async (permission: IPermission) => {
  const result = await execute<{ affectedRows: number }>(PermissionQueries.AddPermission, [
    Object.values(permission)
  ]);
  return result.affectedRows > 0;
};


/**
 * updates permission information based on the id provided
 */
 export const deletePermission = async (id: IPermission['id']) => {
  const result = await execute<{ affectedRows: number }>(PermissionQueries.DeletePermissionById, [
    id
  ]);
  return result.affectedRows > 0;
};